package com.example.utils;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/*
@description:
@ClassName DelayTask
@author chen
@create 2025-08-14 16:15
@Version 1.0
*/
public class DelayTask<T> implements Delayed
{
    // 延迟任务执行时间（时间戳毫秒）
    private long taskExecuteTimeMs;
    private Runnable task;

    public DelayTask(long taskExecuteTimeMs, Runnable task)
    {
        this.taskExecuteTimeMs = taskExecuteTimeMs;
        this.task = task;
    }

    public void setTaskExecuteTimeMs(long taskExecuteTimeMs)
    {
        this.taskExecuteTimeMs = taskExecuteTimeMs;
    }

    public Runnable getTask()
    {
        return task;
    }

    public long getTaskExecuteTimeMs()
    {
        return taskExecuteTimeMs;
    }


    @Override
    public long getDelay(TimeUnit unit)
    {
        return unit.convert(taskExecuteTimeMs - System.currentTimeMillis(), java.util.concurrent.TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o)
    {
        if (o instanceof DelayTask)
        {
            DelayTask o1 = (DelayTask) o;
            return Long.compare(this.getTaskExecuteTimeMs(), o1.getTaskExecuteTimeMs());
        }
        return 0;
    }
}
